package com.hwtx.form.domain.def.meta;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.hwtx.form.domain.FormConstants;
import com.hwtx.form.domain.def.FormDetailExt;
import com.hwtx.form.domain.vo.FormConfigQueryVo;
import com.hwtx.form.persistence.entity.FormDefDetail;
import com.hwtx.form.util.FormUtil;
import io.geekidea.boot.framework.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Table2 extends Component {

    private final Boolean resizable = true;
    private final Boolean bordered = true;
    private final String rowClassNameExpr = "text-md";
    private String source = "$rows";
    private Boolean columnsTogglable = false;
    private Integer indentSize = 20;
    private List<Column> columns;
    private Api quickSaveItemApi;

    public Table2() {
        super("table2");
    }

    public static Table2 build(FormDefDetail formDefDetail, List<FormConfigQueryVo> formConfigQueryList, String serviceName) {
        Table2 table2 = new Table2();
        table2.setId(getComponentId());
        table2.setName(table2.getId());
        table2.columns = Column.buildColumns(formConfigQueryList, false);
        Column opColumn = Operation.build(formConfigQueryList, formDefDetail, serviceName, false);
        if (opColumn != null) {
            table2.columns.add(opColumn);
        } else {
            FormDetailExt formDetailExt = FormUtil.getFormDetailExt(formDefDetail.getExt());
            if (formDetailExt != null &&
                    FormDetailExt.getConfigTabs(formDetailExt.getConfigTabs(), formDefDetail.getFormId()).contains(FormConstants.FormConfigType.FORM)) {
                throw new BusinessException("未设置表单入口，若无需入口，请删除表单页配置");
            }
        }
        table2.columns.add(Column.buildHiddenColumn(FormConstants.DefaultColumn.id.name(), null));
        FormUtil.buildListParams(formDefDetail).forEach((k, v) -> table2.columns.add(Column.buildHiddenColumn(k, v)));
        return table2;
    }
}
